package seg;

import java.awt.geom.Line2D;
import java.util.List;

import doc.SampledPoint;

public class RelativeRho {

	private SampledPoint[] ps;

	private int K;

	public RelativeRho() {

	}

	public void setPoints(List<SampledPoint> points) {
		ps = new SampledPoint[points.size()];
		points.toArray(ps);
	}

	public void setPoints(SampledPoint[] points) {
		ps = points;
	}

	public SampledPoint[] getPoints() {
		return ps;
	}

	public int getK() {
		return K;
	}

	public void setK(int k) {
		K = k;
	}

	public double[] getRhos() {
		return calcRhos(getPoints(), getK());
	}

	public static double[] calcRhos(SampledPoint[] ps, int K) {
		double[] rhos = new double[ps.length];

		for (int i = 0; i < ps.length; i++) {
			rhos[i] = calcRho(ps, i, K);
//			System.out.println("rho at " + i + ":" + rhos[i]);
		}

		return rhos;
	}

	public static double calcRho(SampledPoint[] ps, int I, int K) {
        if (I < K || I >= ps.length - K)
            return 0;

        Line2D line = new Line2D.Double(ps[I-K].getX(), ps[I-K].getY(),
                ps[I+K].getX(), ps[I+K].getY());

        double dis = 0;
        double len = line.getP1().distance(line.getP2());
        for (int i = I - K + 1; i < I + K; i++) {
            dis += Math.abs(line.ptLineDist(ps[i].toPoint2D()));
        }
        return Math.abs(dis/len);
    }
}
